#pragma oce
#include <map>
#include <unordered_map>
#include <string>
#include <fstream>
#include "log.hpp"
#include "InetAddr.hpp"
using namespace LogModule;
const std::string defaultdictpath = "./dict.txt";
class Dict
{
public:
    Dict(std::string path = defaultdictpath)
    {
        _path = path;
    }
    bool LoadDict()
    {
        // 进行读取文件
        std::ifstream in(_path);
        if (!in.is_open())
        {
            LOG(LogLevel::DEBUG) << "打开字典: " << _path << " 错误";
            return false;
        }
        std::string sep(": ");
        std::string tem;
        while (getline(in, tem))
        {
            int pos = tem.find(sep);
            if (pos == std::string::npos)
            {
                LOG(LogLevel::WARNING) << "解析: " << tem << " 失败";
                continue;
            }
            std::string english = tem.substr(0, pos);
            std::string chinese = tem.substr(pos + sep.size());
            if (english.empty() || chinese.empty())
            {
                LOG(LogLevel::WARNING) << "没有有效内容: " << tem;
                continue;
            }
            //std::cout<<english<<std::endl<<chinese<<std::endl;
            _dict.insert(make_pair(english, chinese));
            LOG(LogLevel::DEBUG) << "加载: " << tem;
        }
        return true;
    }
    std::string Translate(const std::string &word, InetAddr &client)
    {
        //std::cout<<word<<std::endl;
        auto iter = _dict.find(word);
        if (iter == _dict.end())
        {
            LOG(LogLevel::DEBUG) << "进入到了翻译模块, [" << client.Ip() << " : " << client.Port() << "]# " << word << "->None";
            return "None";
        }
        LOG(LogLevel::DEBUG) << "进入到了翻译模块, [" << client.Ip() << " : " << client.Port() << "]# " << word << "->" << iter->second;
        return iter->second;
    }
    ~Dict()
    {
    }

private:
    std::unordered_map<std::string, std::string> _dict;
    std::string _path;
};